﻿//using AppCommon;
using BLL.Base;
using CommandService;
using Entity;
using Entity.App;
using Entity.SysEntity.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.App
{
    public class Token : PubMethod
    {
        /// <summary>
        /// Common操作Token
        /// </summary>
        //OperToken token;
        MsgResult Msg;
        Commands cmd;
        OperateModel oper;
        TokenInfo TokenInfo;
        ServiceRecord ServiceRecord;
        //Common tokenCommon;
        public Token()
        {

            //APPcommon
            //token = new OperToken();
            //返回类
            Msg = new MsgResult();
            //sql访问类
            cmd = new Commands();
            //sql操作类
            oper = new OperateModel();
            //token存储类
            TokenInfo = new TokenInfo();
            //移动设备调用接口记录
            ServiceRecord = new ServiceRecord();
            //设备接口记录
            //tokenCommon = new Common();
        }
        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="json">请求串</param>
        /// <returns></returns>
        public string getToken(string json)
        {
            string appCode = "";
            string TimeNow = getNow();
            string ServiceName = "getToken";
            try
            {
                //解密请求串
                json = Tools.Encryption.DesDecrypt(json);
                //格式化json
                JObject jsonObject = JObject.Parse(json);
                if (jsonObject.Property("appCode") != null)
                {
                    if (jsonObject.Property("version") != null)
                    {
                        appCode = jsonObject["appCode"].ToString();
                        string version = jsonObject["version"].ToString();
                        //string tokens = token.getToken();
                        string tokens = "";
                        List<OperateModel> list = new List<OperateModel>();//事务操作数据库
                        ProgramInfo programInfo = new ProgramInfo();
                        programInfo.apCode = appCode;
                        //根据appCode查询移动设备信息表是否存在该设备
                        int Count = cmd.GetDataByObject<ProgramInfo>(programInfo).Count;
                        programInfo.apVersion = version;
                        programInfo.createTime = TimeNow;
                        if (Count > 0)
                        {
                            //根据apCode修改设备信息
                            oper.Data = programInfo;
                            oper.Types = OperateModel.OperateType.Edit;
                            oper.Conditions = "apCode";
                            list.Add(oper);
                        }
                        else
                        {
                            //添加设备信息
                            oper.Data = programInfo;
                            oper.Types = OperateModel.OperateType.Add;
                            list.Add(oper);
                        }
                        oper = new OperateModel();
                        TokenInfo.appCode = appCode;
                        //判断token记录表是否有该设备信息
                        Count = cmd.GetDataByObject<TokenInfo>(TokenInfo).Count;
                        TokenInfo.createtime = TimeNow;
                        TokenInfo.token = tokens;
                        if (Count > 0)
                        {
                            //根据apCode修改Token存储信息
                            oper.Data = TokenInfo;
                            oper.Types = OperateModel.OperateType.Edit;
                            oper.Conditions = "apCode";
                            list.Add(oper);
                        }
                        else
                        {
                            oper.Data = TokenInfo;
                            oper.Types = OperateModel.OperateType.Add;
                            list.Add(oper);
                        }
                        //事务执行sql
                        ReturnResult rsl = cmd.MoreUpdate(list);
                        if (rsl.Result)
                        {
                            Msg.data = tokens;
                            Msg.result = true;
                            Msg.MsgCode = "02001";
                        }
                        else
                        {
                            Msg.Msg = "sql执行错误,请联系开发者。";
                            Msg.result = false;
                            Msg.MsgCode = "03001";
                        }
                    }
                    else
                    {
                        Msg.Msg = "缺少version参数";
                        Msg.result = false;
                        Msg.MsgCode = "01001";
                    }
                }
                else
                {
                    Msg.Msg = "缺少appCode参数";
                    Msg.result = false;
                    Msg.MsgCode = "01001";
                }
            }
            catch (Exception ex)
            {
                Msg.Msg = ex.ToString();
                Msg.result = false;
                Msg.MsgCode = "03001";
            }
            //接口日志
            string result = JsonConvert.SerializeObject(Msg);
            //cmd.Add(tokenCommon.setServiceRecord(appCode, json, TimeNow, ServiceName, result));
            return result;
        }
    }
}
